var h = $(window).height(),
    w = $(window).width(),
    pageIndex = 1,//当前页数
    pageIndexs = 1,//弹出框页数
    ajbh = '',//原案件编号
    ajmc = '',//原案件名称
    ajbhs = [],//案件编号选中串并
    ajmcs = [],//案件名称
    _item = '',//操作项
    scrollBar = [];//每一项位置
$(function(){
    $('.row-hoof').css({height: h + 'px'});
    $('.newStyle').css({height: h - 20 + 'px'});
    $('.caseContent').css({height: h - 370 + 'px'});
    $('.rightContent').css({height: h - 130 + 'px',display: 'none'});
    $('.noContent').css({height: h - 84 + 'px'});
    $(window).resize(function(){
        h = $(window).height();
        $('.row-hoof').css({height: h});
        $('.newStyle').css({height: h - 20 + 'px'});
        $('.caseContent').css({height: h - 370 + 'px'});
        $('.rightContent').css({height: h - 130 + 'px'});
        $('.noContent').css({height: h - 84 + 'px'});
    })
    Echarts();
    btnClick(1);
})
function btnClick(page){
    $.ajax({
        type: 'get',
        url: baseUrls + 'analysis/result',
        data: {
            searchCon: $.trim($('#Input').val()),
            ajlx: $('.ajlx').val(),
            xsdz: $('.xsdz').val(),
            pageIndex: page,
            pageSize: 50
        },
        beforeSend: function(){
            Loading();
        },
        success: function(response){
            closeLoading();
            $('.noContent').css({display:'none'});
            $('.rightContent').css({display:'block'});
            $('.determine').css({display:'block'});
            if(response.responseCode == '1000'){
                if(response.content.list.length > 0){
                    $('.nineUl').html('');
                    $('.eightUl').html('');
                    $('.sevenUl').html('');
                    $('.sixUl').html('');
                    $('.lessThanUl').html('');
                    pageIndex = response.content.pageNum;
                    for(var i = 0; i < response.content.list.length; i++){
                        if(response.content.list[i].xsdz != null && response.content.list[i].xsdz != 0){
                            var anchorItems = [],
                                content = '',
                                editContent = '';//填写内容
                            if(response.content.list[i].xsyjms != null && $.trim(response.content.list[i].xsyjms).length > 0){
                                anchorItems = response.content.list[i].xsyjms.split(',');
                                for(var j = 0; j < anchorItems.length; j++){
                                    content += '<span>'+ anchorItems[j] +'</span>';
                                }
                            }
                            editContent = '<li><span class="rightBorder border" onclick="chooseSelect(this,\''+ response.content.list[i].ajbh +'\',\''+ response.content.list[i].ajmc +'\',\''+ response.content.list[i].xsajbh +'\')"><span class="rightIcon"></span></span>&nbsp&nbsp<span class="width">'+ nulls(response.content.list[i].xsdz) +'</span><span class="ajbh" style="cursor: pointer;" onclick="showDetail(\''+ response.content.list[i].ajbh +'\')">原案件：'+ nulls(response.content.list[i].ajbh) +'</span>&nbsp&nbsp<span class="ajmc">'+ nulls(response.content.list[i].ajmc) +'</span><span class="ajbh" style="cursor: pointer;" onclick="showDetail(\''+ response.content.list[i].xsajbh +'\')">相似案件：'+ nulls(response.content.list[i].xsajbh) +'</span>&nbsp&nbsp<span class="ajmc">'+ nulls(response.content.list[i].xsajmc) +'</span></li>';
                            if(response.content.list[i].xsdz > 90){
                                $('.nineDiv').html(content);
                                $('.nineUl').append(editContent);
                            }else if(response.content.list[i].xsdz > 80){
                                $('.eightDiv').html(content);
                                $('.eightUl').append(editContent);
                            }else if(response.content.list[i].xsdz > 70){
                                $('.sevenDiv').html(content);
                                $('.sevenUl').append(editContent);
                            }else if(response.content.list[i].xsdz > 60){
                                $('.sixDiv').html(content);
                                $('.sixUl').append(editContent);
                            }else{
                                $('.lessThanDiv').html(content);
                                $('.lessThanUl').append(editContent);
                            }
                        }else{
                            console.log('返回值第'+ i + '项有问题');
                        }
                    }
                    scrollBar = [];
                    for(var i = 0; i < $('.over').length; i++){
                        if($('.over').eq(i).find('ul li').length > 0){
                            $('.over').eq(i).css({display: 'block'});
                            $('.anchorDiv').eq(i).css({display: 'block'});
                        }else{
                            $('.over').eq(i).css({display: 'none'});
                            $('.anchorDiv').eq(i).css({display: 'none'});
                        }
                        scrollBar.push($('.over').eq(i).offset().top - 84);
                    }
                    if(response.content.pageNum == response.content.pages){
                        $('.more').hide();
                    }else{
                        $('.more').show();
                    }
                }else{
                    Msg('暂无相似案件');
                    $('.rightContent').css({display: 'none'});
                    $('.determine').css({display: 'none'});
                    $('.noContent').css({display:'block'});
                }
            }else{
                Msg('相似案件获取失败');
                $('.rightContent').css({display: 'none'});
                $('.determine').css({display: 'none'});
                $('.noContent').css({display:'block'});
            }
        },
        error: function(){
            closeLoading();
            Msg('相似案件获取失败');
            $('.rightContent').css({display: 'none'});
            $('.determine').css({display: 'none'});
            $('.noContent').css({display:'block'});
        }
    })
}
//获取相似案件
function getMsg(){
    if(ajbh != ''){
        ajbhs = [];
        ajmcs = [];
        layer.open({
            type: 1,
            title: ['相似案件列表','background: #2180d4;color:#fff'],
            shadeClose: true,
            shade: 0.8,
            scrollbar: false,
            area: ['90%', '90%'],
            offset: 'auto',
            content: $("#sameCase"),
            success: function(layero, index){
                $.ajax({
                    type: 'get',
                    url: baseUrls + 'analysis/result/' + ajbh.split(';')[0],
                    data: {
                        pageIndex: pageIndexs,
                        pageSize: 50
                    },
                    beforeSend: function(){
                        Loading();
                    },
                    success: function(response){
                        closeLoading();
                        if(response.responseCode == '1000'){
                            if(response.content.list.length > 0){
                                $('.nineUls').html('');
                                $('.eightUls').html('');
                                $('.sevenUls').html('');
                                $('.sixUls').html('');
                                $('.lessThanUls').html('');
                                pageIndex = response.content.pageNum;
                                for(var i = 0; i < response.content.list.length; i++){
                                    if(response.content.list[i].xsdz != null && response.content.list[i].xsdz != 0){
                                        var anchorItems = [],
                                            liClass = '',//默认选中
                                            content = '',
                                            editContent = '';//填写内容
                                        if(response.content.list[i].xsyjms != null && $.trim(response.content.list[i].xsyjms).length > 0){
                                            anchorItems = response.content.list[i].xsyjms.split(',');
                                            for(var j = 0; j < anchorItems.length; j++){
                                                content += '<span>'+ anchorItems[j] +'</span>';
                                            }
                                        }
                                        if(ajbh.split(';')[1] == response.content.list[i].ajbh){
                                            liClass = 'selectedBg';
                                            ajbhs.push(response.content.list[i].ajbh);
                                            ajmcs.push(response.content.list[i].ajmc);
                                        }
                                        editContent = '<li class="sameCaseLi"><span class="rightBorder border '+ liClass +'" onclick="chooseSelects(this,\''+ response.content.list[i].ajbh +'\',\''+ response.content.list[i].ajmc +'\')"><span class="rightIcon"></span></span>&nbsp&nbsp<span class="ajbh" style="cursor: pointer;" onclick="showDetail(\''+ response.content.list[i].ajbh +'\')">'+ nulls(response.content.list[i].ajbh) +'</span><span class="width">'+ nulls(response.content.list[i].xsdz) +'</span><span class="ajmc">'+ nulls(response.content.list[i].ajmc) +'</span></li>';
                                        if(response.content.list[i].xsdz > 90){
                                            $('.nineDivs').html(content);
                                            $('.nineUls').append(editContent);
                                        }else if(response.content.list[i].xsdz > 80){
                                            $('.eightDivs').html(content);
                                            $('.eightUls').append(editContent);
                                        }else if(response.content.list[i].xsdz > 70){
                                            $('.sevenDivs').html(content);
                                            $('.sevenUls').append(editContent);
                                        }else if(response.content.list[i].xsdz > 60){
                                            $('.sixDivs').html(content);
                                            $('.sixUls').append(editContent);
                                        }else{
                                            $('.lessThanDivs').html(content);
                                            $('.lessThanUls').append(editContent);
                                        }
                                    }else{
                                        console.log('返回值第'+ i + '项有问题');
                                    }
                                }
                                if(response.content.pageNum == response.content.pages){
                                    $('.mores').hide();
                                }else{
                                    $('.mores').show();
                                }
                                for(var i = 0; i < $('.overs').length; i++){
                                    if($('.overs').eq(i).find('ul li').length > 0){
                                        $('.overs').eq(i).css({display: 'block'});
                                        $('.anchorDivs').eq(i).css({display: 'block'});
                                    }else{
                                        $('.overs').eq(i).css({display: 'none'});
                                        $('.anchorDivs').eq(i).css({display: 'none'});
                                    }
                                }
                            }else{
                                Msg();
                            }
                        }else{
                            Msg();
                        }
                    },
                    error: function(){
                        closeLoading();
                        Msg();
                    }
                })
            },
            end: function(){
            }
        });
    }else{
        Msg('请先选择案件');
    }
}
function nulls(item){
    var callback;
    if(item != null && item != undefined && $.trim(item).length > 0){
        callback = item;
    } else {
        callback = '无';
    }
    return callback;
}
function keyPress(event){
    event = event ? event : window.event;
    if(event.keyCode == '13'){
        btnClick();
        $('#Input').blur();
    }
}
//echarts
function Echarts(){
    var noContent = echarts.init(document.getElementById('noContent'));
    function createRandomItemStyle() {
        return {
            normal: {
                color: 'rgb(' + [
                    Math.round(Math.random() * 160),
                    Math.round(Math.random() * 160),
                    Math.round(Math.random() * 160)
                ].join(',') + ')'
            }
        };
    }
    var option = {
        tooltip: {
            show: false
        },
        series: [{
            name: '',
            type: 'wordCloud',
            size: ['80%', '80%'],
            textRotation : [0, 45, 90, -45],
            textPadding: 0,
            autoSize: {
                enable: true,
                minSize: 14
            },
            data: [
                {
                    name: "诈骗案",
                    value: 10000,
                    itemStyle: {
                        normal: {
                            color: 'black'
                        }
                    }
                },
                {
                    name: "相似度",
                    value: 6181,
                    itemStyle: createRandomItemStyle()
                },
                {
                    name: "线索统计",
                    value: 4386,
                    itemStyle: createRandomItemStyle()
                },
                {
                    name: "手动研判",
                    value: 4055,
                    itemStyle: createRandomItemStyle()
                },
                {
                    name: "常住人口",
                    value: 2467,
                    itemStyle: createRandomItemStyle()
                },
                {
                    name: "抢夺案",
                    value: 2244,
                    itemStyle: createRandomItemStyle()
                },
                {
                    name: "其他",
                    value: 1898,
                    itemStyle: createRandomItemStyle()
                },
                {
                    name: "简要案情",
                    value: 1484,
                    itemStyle: createRandomItemStyle()
                },
                {
                    name: "相似案件",
                    value: 1112,
                    itemStyle: createRandomItemStyle()
                },
                {
                    name: "火车站",
                    value: 965,
                    itemStyle: createRandomItemStyle()
                },
                {
                    name: "相似案件",
                    value: 847,
                    itemStyle: createRandomItemStyle()
                },
                {
                    name: "其他",
                    value: 582,
                    itemStyle: createRandomItemStyle()
                },
                {
                    name: "抢劫案",
                    value: 555,
                    itemStyle: createRandomItemStyle()
                },
                {
                    name: "抢盗",
                    value: 550,
                    itemStyle: createRandomItemStyle()
                },
                {
                    name: "吸毒",
                    value: 462,
                    itemStyle: createRandomItemStyle()
                },
                {
                    name: "强奸",
                    value: 366,
                    itemStyle: createRandomItemStyle()
                },
                {
                    name: "诈骗",
                    value: 360,
                    itemStyle: createRandomItemStyle()
                },
                {
                    name: "火车站",
                    value: 282,
                    itemStyle: createRandomItemStyle()
                },
                {
                    name: "相似度",
                    value: 273,
                    itemStyle: createRandomItemStyle()
                },
                {
                    name: "火车站",
                    value: 265,
                    itemStyle: createRandomItemStyle()
                }
            ]
        }]
    };
    noContent.setOption(option);
}
//锚点设置
function anchor(item){
    var index = $(item).index();
    $('.rightContent').animate({
        scrollTop: scrollBar[index]
    })
}
//分页右侧
function page(num){
    if(num){//代表为弹出框分页获取数据
        pageIndexs += 1;
        getMsg(pageIndexs);
    }else{
        pageIndex += 1;
        btnClick(pageIndex);
    }
}
//多选
function chooseSelects(item,ajbh,ajmc){
    $(item).toggleClass('selectedBg');
    if ($(item).hasClass('selectedBg')) {
        ajbhs.push(ajbh);
        ajmcs.push(ajmc);
    } else {
        var Index = ajbhs.indexOf(ajbh);
        ajbhs.splice(Index,1);
        ajmcs.splice(Index,1);
    }
}
//单选
function chooseSelect(item,bh,mc,xsajbh){
    var isChoose = false;
    if($(item).hasClass('selectedBg')){
        isChoose = true;
    }
    $('.rightContent').find('.rightBorder ').removeClass('selectedBg');
    if(isChoose){
        $(item).removeClass('selectedBg');
        ajbh = '';
        ajmc = '';
    }else{
        $(item).addClass('selectedBg');
        ajbh = bh + ';' + xsajbh;
        ajmc = mc;
    }
}
//研判数据填充
//研判
function caseCade(item){
    if(ajbhs.length > 0){
        $.ajax({
            type: 'get',
            cache: false,
            url: baseUrls + 'analysis/xsajs',
            data: {
                ajbh: ajbh.split(';')[0],
                xsajbh: ajbhs.join(',')
            },
            beforeSend: function(){
                Loading();
            },
            success: function(response){
                closeLoading();
                if(response.responseCode == '1000'){
                    $('.caseCadeContent').html('');
                    var dbx = ['案件类型','案件类别','案件性质','案发开始时间','案发结束时间','案情关键词','选择对象描述','作案时段描述','作案进口描述','侵入方式描述','作案出口描述','作案工具描述','作案地点','简要案情'];//对比项
                    response.content.forEach(function(item,index){
                        var  ajbhData = '',//数据填充
                             delButton = '<i class="pull-right fa fa-times-circle red i" aria-hidden="true" onclick="del(this,\''+ item.AJBH +'\')"></i>';//删除按钮
                        if(index == 0){
                            delButton = '';
                        }
                        dbx.forEach(function(items,indexs){
                            var commonClass = '';
                            if(item.XSYJMS.indexOf(items) >= 0){
                                commonClass = 'redLi'
                            }
                            if(items == '案件类型'){
                                ajbhData += '<li class="liUlLi '+ commonClass +'"><div class="left">案件类型</div><div class="right">'+ nulls(item.AJLX) +'</div></li>';
                            }
                            if(items == '案件类别'){
                                ajbhData += '<li class="liUlLi '+ commonClass +'"><div class="left">案件类别</div><div class="right">'+ nulls(item.AJLB) +'</div></li>';
                            }
                            if(items == '案件性质'){
                                ajbhData += '<li class="liUlLi '+ commonClass +'"><div class="left">案件性质</div><div class="right">'+ nulls(item.AJXZ) +'</div></li>';
                            }
                            if(items == '案发开始时间'){
                                ajbhData += '<li class="liUlLi '+ commonClass +'"><div class="left">案发开始时间</div><div class="right">'+ nulls(item.AFKSSJ) +'</div></li>';
                            }
                            if(items == '案发结束时间'){
                                ajbhData += '<li class="liUlLi '+ commonClass +'"><div class="left">案发结束时间</div><div class="right">'+ nulls(item.AFJSSJ) +'</div></li>';
                            }
                            if(items == '案情关键词'){
                                ajbhData += '<li class="liUlLi '+ commonClass +'"><div class="left">案情关键词</div><div class="right">'+ nulls(item.AQGJC) +'</div></li>';
                            }
                            if(items == '选择对象描述'){
                                ajbhData += '<li class="liUlLi '+ commonClass +'"><div class="left">选择对象描述</div><div class="right">'+ nulls(item.XZDXMS) +'</div></li>';
                            }
                            if(items == '作案时段描述'){
                                ajbhData += '<li class="liUlLi '+ commonClass +'"><div class="left">作案时段描述</div><div class="right">'+ nulls(item.ZASDMS)+'</div></li>';
                            }
                            if(items == '作案进口描述'){
                                ajbhData += '<li class="liUlLi '+ commonClass +'"><div class="left">作案进口描述</div><div class="right">'+ nulls(item.ZAJKMS) +'</div></li>';
                            }
                            if(items == '侵入方式描述'){
                                ajbhData += '<li class="liUlLi '+ commonClass +'"><div class="left">侵入方式描述</div><div class="right">'+ nulls(item.QRFSMS) +'</div></li>';
                            }
                            if(items == '作案出口描述'){
                                ajbhData += '<li class="liUlLi '+ commonClass +'"><div class="left">作案出口描述</div><div class="right">'+ nulls(item.ZACKMS) +'</div></li>';
                            }
                            if(items == '作案工具描述'){
                                ajbhData += '<li class="liUlLi '+ commonClass +'"><div class="left">作案工具描述</div><div class="right">'+ nulls(item.ZAGJMS) +'</div></li>';
                            }
                            if(items == '作案地点'){
                                ajbhData += '<li class="liUlLi '+ commonClass +'"><div class="left">作案地点</div><div class="right">'+ nulls(item.ZADD) +'</div></li>';
                            }
                            if(items == '简要案情'){
                                ajbhData += '<li class="liUlLi jyaqLi '+ commonClass +'"><div class="left">简要案情</div><div class="right" id="right'+ index +'" onmouseenter="tips(\''+ nulls(item.JYAQ) +'\',\'right'+ index +'\')">'+ nulls(item.JYAQ) +'</div></li>';
                            }
                        });
                        $('.caseCadeContent').append('<li class="caseCadeLi">' +
                            '<h3><span class="h3Span" id="'+ index +'"  onmouseenter="tips(\''+ nulls(item.AJMC) +'\','+ index +')">'+ item.AJMC +' </span>'+ delButton +'</h3>' +
                            '<ul class="liUl margin padding">'+
                            '<li class="liUlLi">'+
                            ajbhData +
                            '</li>'+
                            '</ul>'+
                            '</li>');
                    });
                    caseCadeDetail();
                }else{
                    Msg();
                }
            },
            error: function(){
                closeLoading();
                Msg();
                caseCadeDetail();
            }
        });
    }else{
        Msg('请选择案件后串并',5);
    }
}
//串并详细信息
function caseCadeDetail(){
    layer.open({
        type: 1,
        area: ['80%','80%'],
        resize: false,
        content: $('#caseCade')
    });
}
//串并 按钮
function sureCaseCode(){
    if($.trim($('.glmc').val()) != '' || $.trim($('.glmc').val()).length > 20){
        if($.trim($('.cbly').val()) != ''){
            layer.confirm('确定串并？', {
                btn: ['确定','取消'] //按钮
            }, function(){
                $.ajax({
                    type: 'post',
                    cache: false,
                    url: baseUrls + 'homepage/glaj',
                    contentType: "application/json",
                    dataType: "json",
                    data: JSON.stringify({
                        ajbh: ajbh.split(';')[0],
                        ajmc: ajmc,
                        ajbhs: ajbhs.join(','),
                        ajmcs: ajmcs.join('~'),
                        cjr: $('#userCode').val(),
                        glmc: $('.glmc').val(),
                        ms: $('.cbly').val(),
                        roleEnName: $('#roleEnName').val()
                    }),
                    beforeSend: function(){
                        Loading();
                    },
                    success: function(response){
                        closeLoading();
                        if(response.responseCode == '1000'){
                            Msg('串并成功',1);
                            setTimeout(function(){
                                ajbh = '';
                                ajmc = '';
                                ajbhs = [];
                                ajmcs = [];
                                $(".rightBorder").removeClass("selectedBg");
                                layer.closeAll();
                            },1000)
                        }
                    },
                    error: function(){
                        closeLoading();
                        Msg();
                        layer.closeAll();
                    }
                })
            }, function(){
            });
        }else{
            Msg('请输入串并理由！');
        }
    }else{
        Msg('请输入案件名称，且长度不能超过20个字符！');
    }
}
//删除
function del(item,ajbh){
    var Index = ajbhs.indexOf(ajbh);
    ajbhs.splice(Index,1);
    $(item).parents('li').remove();
    if(ajbhs.length == 0){
        $(".rightBorder").removeClass("selectedBg");
        layer.closeAll();
    }
}
//详情
function showDetail(qzbh){
    layer.open({
        type: 2,
        area: ['90%', h * 0.9 + 'px'],
        fixed: false, //不固定
        maxmin: false,
        content: 'layer/layer.html?' + qzbh + '&' + $('#userId').val()
    });

}